package com.codebattery.service;


import com.codebattery.domain.entity.OrderItem;
import com.codebattery.util.CatchImgUtil;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Service
public class OrderImgSyncService {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderImgSyncService.class);
    @Resource
    private OrderItemService orderItemService;
    @Resource
    private MinioClient minioClient;

    public void syncImgs(){

//        ArrayBlockingQueue<Runnable> syncQueue = new ArrayBlockingQueue<Runnable>(50);
//
//        ThreadPoolExecutor executor = new ThreadPoolExecutor(30, 100, 300, TimeUnit.SECONDS,
//                syncQueue);
//
//        List<OrderItem> orderItems = orderItemService.find((root, query, cb)->{
//            return cb.notLike(root.get("productPic"), "%%192.168.3.31%");
//        });
//
//        for(OrderItem orderItem : orderItems){
//            executor.execute(()->{
//                String productPicUrl = orderItem.getProductPic();
//                if(StringHelper.isNotEmpty(productPicUrl)){
//                    try {
//                        String[] splits = productPicUrl.split("/");
//
//                        if(splits.length > 0 && StringHelper.isNotEmpty(splits[splits.length-1])){
//                            InputStream imgStream = CatchImgUtil.getImgStream(productPicUrl);
//                            PutObjectOptions options = new PutObjectOptions(imgStream.available(), -1);
//
//
//                            minioClient.putObject("1-mall",
//                                    "product/" + splits[splits.length-1],
//                                    imgStream, options);
//
//                            String url = minioClient.getObjectUrl("1-mall", "product/" + splits[splits.length-1]);
//                            orderItem.setProductPic(url);
//
//                            orderItemService.update(orderItem);
//                            imgStream.close();
//                        }
//                        LOGGER.info("OrderItem id=[" + orderItem.getId() + "]成功, "
//                                + "url=[" + orderItem.getProductPic() + "]");
//
//                    } catch (Exception e) {
//                        LOGGER.error("OrderItem id=[" + orderItem.getId() + "]失败, "
//                        + "url=[" + orderItem.getProductPic() + "]");
//                        e.printStackTrace();
//                    }
//                }
//            });
//        }
//
//        executor.shutdown();
    }

}
